<?php

require_once "Basic.php";

class Filial {
  
  public static function getAll($db = null) {
    $wasNull = false;
    if($db === null) {
      $db = new DBUtils();
      $db->connect();
      $wasNul = true;
    }
    
    $db->runQuery("select * from DadosFilial");
    $all = array();
    for($item = $db->getItem(); $item !== null; $item = $db->getItem()) {
      $all[$item->idFilial] = $item;
    }
    
    if($wasNul) {
      $db->close();
    }
    
    return $all;
  }
  
  public static function getAllForChoice($db = null) {
    $all = Filial::getAll($db);
    foreach($all as $k => &$v) {
      $address = Endereco::montar($v->logradouro, $v->numero, $v->CEP, $v->bairro, $v->nomeCidade, $v->nomeUF);
      if($address !== "") {
        $v = $v->nome . " (${address})";
      }
      else {
        $v = $v->nome;
      }
    }
    return $all;
  }
  
  
}

